<%@ page language="java" pageEncoding="UTF-8" contentType="text/html;charset=UTF-8" %>
<%@ include file="/resources/modular/plugin/taglibs.jsp" %>
<!doctype html>
<head content="-库存列表">
    <%@ include file="/resources/modular/head/inside_head.jsp" %>
</head>
<body class="GC_Menu_5" name="GC_inventory">

<%@ include file="/resources/modular/plugin/mainMenu.jsp" %>

<div class="page">
    <%@ include file="/resources/modular/plugin/mainTopMenu.jsp" %>

    <div class="page_left combopage_left">
        <ul class="combopage_leftu1">
            <c:forEach items="${placeList}" var="data">
                <li><a href="javascript:;" name="${data.placeid}"
                       class="${data.placeid eq placeId?'cur':''}">${data.address}</a></li>
            </c:forEach>
        </ul>
    </div>

    <div class="page_right">
        <ul class="inventorylist_equipmentlist">
            <c:forEach items="${giftList}" var="data" varStatus="index">
                <li>
                    <p>
                        <i>${index.count}</i>
                        <span class="inventorylist_equipmentName"><span>${data.deviceName}</span>&nbsp;&nbsp;${data.deviceShortCode}</span>
                        <span class="inventorylist_inventory">剩余礼品：<span>${data.giftqty}</span></span>
                        <span class="inventorylist_equipmentImg">
                        	<img class="imgPath" src="${data.giftimgurl}" alt="">
                            <span>${data.giftname}</span>
                        </span>
                        <a class="inventorylist_edit" href="javascript:;">编辑</a>
                    </p>

                    <div class="inventorylist_equipment_edit">
                        <input type="hidden" class="giftId" value="${data.giftid}">
                        <span class="inventorylist_equipmentNameCur">
                            <span>${data.deviceName}</span>&nbsp;&nbsp;${data.deviceShortCode}
                        </span>
                        <span class="inventorylist_inventoryCur">剩余礼品：
                            <input class="giftQty" type="number" value="${data.giftqty}"></span>
                        <span class="inventorylist_equipmentImg1">
                        	<img class="imgPath" src="${data.giftimgurl}" alt="">
                            <input class="inventorylist_equipmentImgName" type="text" value="${data.giftname}">
                        	<span class="inventorylist_equipmentImgCur">更换图片
                                <input type="file" name="imageFile" multiple="multiple" id="inputfile"></span>
                        </span>

                        <span class="inventorylist_editbtn">
                        	<a class="inventorylist_editCur" href="javascript:;">确认</a><br>
                        	<a class="inventorylist_editCurno" href="javascript:;">取消</a>
                        </span>
                    </div>
                </li>
            </c:forEach>
        </ul>
    </div>
</div>

</body>

<script type="application/javascript">
    var regName = /^\S{1,6}$/;

    $(".combopage_leftu1 a").each(function (n) {
        $(this).click(function () {
            var place = $(this).attr("name");
            window.location.href = "${basePath}v2/inventory/" + place + "/placeView";
        });
    });

    //更新礼品
    $(".inventorylist_editCur").on("click", function () {
        var giftId = parseInt($(this).parent().parent().find(".giftId").val());
        var giftQty = parseInt($(this).parent().parent().find(".giftQty").val());
        var giftName = $(this).parent().parent().find(".inventorylist_equipmentImgName").val();

        if (!regName.test(giftName)) {
            alert("礼品名称长度不能大于6位!");
            return;
        }

        $.ajax({
            url: '${basePath}v2/inventory/update',
            type: 'POST',
            data: {
                giftId: giftId,
                giftQty: giftQty,
                giftName: giftName
            },
            success: function (ret) {
                alert(ret.info);
            }
        });
    });

    //更换图片
    $("#inputfile").change(function () {
        var giftId = parseInt($(this).parents(".inventorylist_equipment_edit").find(".giftId").val());
        console.log(giftId);
        //创建FormData对象
        var data = new FormData();
        //为FormData对象添加数据
        $.each($('#inputfile')[0].files, function (i, file) {
            data.append('imageFile', file);
        });
        $.ajax({
            url: '${basePath}v2/inventory/' + giftId + '/updateGiftImg',
            type: 'POST',
            data: data,
            cache: false,
            contentType: false,
            processData: false,
            success: function (ret) {
                if (ret.result) {
                    $(".imgPath").attr("src", ret.data);
                } else {
                    alert(ret.info);
                }
            }
        });
    });

</script>
</html>
<%@ include file="/resources/modular/foot/inside_footer.jsp" %>
